package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    private RegionRepository regionRepository;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;

    @GetMapping("/hotRegion")
    public  Object hotRegion(){
        List<Region> list = regionRepository.findAll();
        return JsonResult.success(list);
    }

    @GetMapping("/destsByRegionId")
    public  Object destsByRegionId(String regionId){
        List<Destination> list = destinationService.getDestByRegionIdForApi(regionId);
        return JsonResult.success(
                new ParamMap()
                        .put("list",list)
                        .put("regionId",regionId)
        );
    }
    @GetMapping("/getToast")
    public  Object getToast(String id){
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = destinationService.get(id);
        return JsonResult.success(
                new ParamMap()
                        .put("toasts",toasts)
                        .put("dest",dest)
        );
    }

    @GetMapping("/detail")
    public  Object detail(String id){
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.findByDestId(id);
        //strategies  点击量前三的攻略文章
        StrategyQuery qo = new StrategyQuery();
        //使用qo传参
        qo.setDestId(id);
        //使用qo里的pageable来设置排序  和  显示hide数量
        PageRequest of = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        //设置分页排序规则
        qo.setPageable(of);
        Page list = strategyService.list(qo);
        return JsonResult.success(
                new ParamMap()
                        .put("toasts",toasts)
                        .put("dest",dest)
                        .put("catalogs",catalogs)
                        .put("strategies",list.getContent())
        );
    }
}





